package com.weaver.qfengx.map;

import java.util.HashMap;
import java.util.Map;

/**
 * 大小写不敏感的map
 */
public class CaseInsensitiveMap<K, V> extends CustomKeyMap<K, V> {

    private static final long serialVersionUID = 1L;

    public CaseInsensitiveMap() {
        this(DEFAULT_INITIAL_CAPACITY);
    }

    public CaseInsensitiveMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }

    public CaseInsensitiveMap(Map<? extends K, ? extends V> m) {
        this(DEFAULT_LOAD_FACTOR, m);
    }

    public CaseInsensitiveMap(int initialCapacity, float loadFactor) {
        super(new HashMap<>(initialCapacity, loadFactor));
    }

    public CaseInsensitiveMap(float loadFactor, Map<? extends K, ? extends V> m) {
        this(m.size(), loadFactor);
        this.putAll(m);
    }


    @Override
    protected Object customKey(Object key) {
        if (key instanceof CharSequence) {
            key = key.toString().toLowerCase();
        }
        return key;
    }
}
